<?php

namespace Admin\Controller;

use Common\Controller\BaseController;

/**
 * 基地管理
 */
class BasesController extends BaseController {

    /**
     * 基地列表
     */
    public function baseList() {
        $pageSize = I('post.pageSize', C('PAGE_SIZE'));
        $pageCurrent = I('post.pageCurrent', 1);
        $orderField = I('post.orderField', 'id');
        $orderDirection = I('post.orderDirection', 'DESC');

        $name = I('post.name', '');

        //参数数组
        $params = array();
        //构造WHERE条件
        $where = '1=1 ';
        if (!empty($name)) {
            $params['name'] = $name;
            $where .= "AND `name` LIKE '%{$name}%' ";
        }

        $base = M('Base');
        //数据总数
        $total = $base->where($where)->count();

        //数据列表
        $list = $base->where($where)
                ->order("{$orderField} {$orderDirection}")
                ->page("{$pageCurrent},{$pageSize}")
                ->select();

        if (!empty($list)) {
            foreach ($list as &$item) {
                $item['add_time'] = $item['add_time'] ? date('Y-m-d H:i:s', intval($item['add_time'])) : '';
            }
        }

        $this->assign('pageSize', $pageSize);
        $this->assign('pageCurrent', $pageCurrent);
        $this->assign('orderField', $orderField);
        $this->assign('orderDirection', $orderDirection);
        $this->assign('total', $total);
        $this->assign('list', $list);
        $this->assign('params', $params);
        $this->display();
    }

    /**
     * 添加基地
     */
    public function addBase() {
        if (IS_POST) {
            $name = I('post.name', '');
            $base = M('Base');

            if (empty($name)) {
                ajaxReturn('基地名称不能为空', 300);
            }

            $existName = $base->where("name='{$name}'")->count();
            if ($existName) {
                ajaxReturn('该基地名称已存在', 300);
            }

            $data = $base->create();
            if ($data) {
                $data['add_time'] = time();
                $data['upd_time'] = time();

                $ret = $base->add($data);

                if ($ret) {
                    ajaxReturn('添加基地成功', 200, '', '', '', 'Admin/Bases/baseList');
                } else {
                    ajaxReturn('添加基地失败', 300);
                }
            } else {
                ajaxReturn('创建数据对象失败', 300);
            }
        } else {

            $this->display();
        }
    }

    /**
     * 修改基地
     */
    public function updBase() {
        if (IS_POST) {
            $name = I('post.name', '');
            $baseId = I('post.baseId', 0);
            $base = M('Base');

            if (empty($baseId)) {
                ajaxReturn('基地ID不能为空', 300);
            }

            if (empty($name)) {
                ajaxReturn('基地名称不能为空', 300);
            }

            $existName = $base->where("name='{$name}' AND id!={$baseId}")->count();
            if ($existName) {
                ajaxReturn('该基地名称已存在', 300);
            }

            $data = $base->create();
            if ($data) {
                $data['upd_time'] = time();
                $ret = $base->where("id={$baseId}")->save($data);

                if ($ret !== false) {
                    ajaxReturn('修改基地成功', 200, '', '', '', 'Admin/Bases/baseList');
                } else {
                    ajaxReturn('修改基地失败', 300);
                }
            } else {
                ajaxReturn('创建数据对象失败', 300);
            }
        } else {
            $baseId = I('get.baseId', '');

            if (empty($baseId)) {
                ajaxReturn('基地ID不能为空', 300);
            }

            $baseInfo = M('Base')->where("id={$baseId}")->find();
            if (empty($baseInfo)) {
                ajaxReturn('没有找到该基地信息', 300);
            }

            $this->assign('baseInfo', $baseInfo);
            $this->display();
        }
    }

    /**
     * 删除基地
     */
    public function delBase() {
        $ids = I('get.ids', '');

        if (empty($ids)) {
            ajaxReturn('请选择删除项', 300);
        }

        $idsArr = explode(',', $ids);
        if (!empty($idsArr)) {
            foreach ($idsArr as $baseId) {
                $hasBlock = M('Block')->where("base_id={$baseId}")->count();
                if ($hasBlock) {
                    ajaxReturn("基地ID为{$baseId}的有地块,不能删除", 300);
                }
            }
        }

        $ret = M('Base')->where("FIND_IN_SET(id,'{$ids}')")->delete();
        if ($ret !== false) {
            ajaxReturn('删除成功', 200, '', '', '', 'Admin/Bases/baseList');
        } else {
            ajaxReturn('删除失败', 300);
        }
    }

    /**
     * 上传缩略图
     */
    public function uploadThumb() {
        $info = uploadOne($_FILES['file'], array('savePath' => 'thumb/'));
        if (!$info) {
            ajaxReturn('上传失败', 300);
        } else {
            $filePath = C('UPLOAD_CONFIG.rootPath') . $info['savepath'] . $info['savename'];
            if (aliossUploadOne($filePath)) {
                ajaxReturn('上传成功', 200, array('filename' => getImgUrl($filePath)));
            } else {
                ajaxReturn('上传失败', 300);
            }
        }
    }

}
